using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

using Oxbow.Tools;
using uk.co.aspellclark.common.ipaq;

namespace uk.co.aspellclark.todolistppc
{
    /// <summary>
    ///   <name>FrmSettings</name>
    ///   <namespace>uk.co.aspellclark.todolistppc</namespace>
    ///   <version>1.0</version>
    ///   <author>Andy Aspell-Clark</author>
    ///   <copyright>2004 Andy Aspell-Clark</copyright>
    ///   <remarks>Preferences dialog</remarks>
    ///   <description>display a dialog to allow users to change program
    /// 				settings
    ///   </description>
    ///   <history>
    ///     <historyitem> 6 Dec 2004  1.0 ARA  Initial Version.</historyitem>
    ///   </history>
    /// </summary>
	public class FrmSettings : System.Windows.Forms.Form, IDisposable
	{
        private System.Windows.Forms.MainMenu m_mnuMain;
        private Microsoft.WindowsCE.Forms.InputPanel m_inputPanel;

        private System.Windows.Forms.Button m_btnCancel;
		private System.Windows.Forms.CheckBox debugEnabled;
		private System.ComponentModel.IContainer components;
        private DialogResult m_drResult = DialogResult.OK;
        private System.Drawing.Font m_Font;

        
		private System.Windows.Forms.ComboBox cboxFont;
		private System.Windows.Forms.ComboBox cboxSize;
		private System.Windows.Forms.TextBox textSample;
		private System.Windows.Forms.ComboBox cboxStyle;

		
        #region Properties
        /// <summary>
        /// returns the result of the dialog (OK or Cancel) signifying which button was pressed to close the dialog
        /// </summary>
        public DialogResult GetDialogResult
        {
            get { return m_drResult; }
            set { m_drResult = value; }
        }
        /// <summary>
        /// returns the name of the font that the user selected
        /// </summary>
        public string SelectedFontName
        {
        	set
        	{
        		cboxFont.SelectedItem = value;
        	}
        	get
        	{
        		object selObj = cboxFont.SelectedItem;
        		if ( selObj != null)
        		{
	        		TextLogger.Log("FontName : " + selObj.ToString());
	        		return (selObj.ToString());
        		}
        		return Constants.DEFAULT_APP_FONT_NAME;
        	}
        }
        /// <summary>
        /// returns the size of the font that the user selected
        /// </summary>
        public string SelectedFontSize
        {
        	set
        	{
        		cboxSize.SelectedItem = value;
        	}
        	get
        	{
        		object selObj = cboxSize.SelectedItem;
        		if ( selObj != null)
        		{
	        		TextLogger.Log("FontSize : " + selObj.ToString());
	        		return (selObj.ToString());
        		}
        		return Constants.DEFAULT_APP_FONT_SIZE;
        	}
        }
        /// <summary>
        /// returns the style of the font that the user selected
        /// </summary>
        public string SelectedFontStyle
        {
        	set
        	{
        		cboxStyle.SelectedItem = value;
        	}
        	get
        	{
        		object selObj = cboxStyle.SelectedItem;
        		if ( selObj != null)
        		{
	        		TextLogger.Log("FontStyle : " + selObj.ToString());
	        		return (selObj.ToString());
        		}
        		return Constants.DEFAULT_APP_FONT_STYLE;
        	}
        }
        /// <summary>
        /// returns the size of the font that the user selected
        /// </summary>
        public bool DebugEnabled
        {
            get { return debugEnabled.Checked;  }
            set { debugEnabled.Checked = value; }
        }

        #endregion



        /// <summary>
        /// 
        /// </summary>
        /// <param name="pFont"></param>
        /// <param name="piParentHeight"></param>
        public FrmSettings(System.Drawing.Font pFont, int piParentHeight)
		{
			//
			// Required for Windows Form Designer support
			//
            InitializeComponent();
            this.ControlBox = false;
            m_Font = pFont;
            this.Font = pFont;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
        }

        private void m_btnCancel_Click(object sender, System.EventArgs e)
        {
            m_inputPanel.Enabled = false;
            m_drResult = DialogResult.Cancel;
            this.Close();
        }//m_btnCancel_Click()

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.cboxFont = new System.Windows.Forms.ComboBox();
			this.cboxSize = new System.Windows.Forms.ComboBox();
			this.textSample = new System.Windows.Forms.TextBox();
			this.cboxStyle = new System.Windows.Forms.ComboBox();
			this.m_inputPanel = new Microsoft.WindowsCE.Forms.InputPanel();
			this.m_mnuMain = new System.Windows.Forms.MainMenu();
			this.m_btnCancel = new System.Windows.Forms.Button();
			this.debugEnabled = new System.Windows.Forms.CheckBox();
			this.SuspendLayout();
			// 
			// cboxFont
			// 
			this.cboxFont.Size = new System.Drawing.Size(128, 22);
			this.cboxFont.SelectedIndexChanged += new System.EventHandler(this.cboxFont_SelectedIndexChanged);
			// 
			// cboxSize
			// 
			this.cboxSize.Items.Add("8");
			this.cboxSize.Items.Add("10");
			this.cboxSize.Items.Add("12");
			this.cboxSize.Items.Add("14");
			this.cboxSize.Items.Add("16");
			this.cboxSize.Items.Add("18");
			this.cboxSize.Location = new System.Drawing.Point(128, 0);
			this.cboxSize.Size = new System.Drawing.Size(40, 22);
			this.cboxSize.SelectedIndexChanged += new System.EventHandler(this.cboxSize_SelectedIndexChanged);
			// 
			// textSample
			// 
			this.textSample.Location = new System.Drawing.Point(16, 40);
			this.textSample.Multiline = true;
			this.textSample.TextAlign = HorizontalAlignment.Center;
			this.textSample.Size = new System.Drawing.Size(208, 216);
			this.textSample.Text = "Todolist PPC Sample Text\r\nThe quick brown cat\r\njumped over the lazy dog";
			// 
			// cboxStyle
			// 
			this.cboxStyle.Items.Add("Regular");
			this.cboxStyle.Items.Add("Bold");
			this.cboxStyle.Items.Add("Italic");
			this.cboxStyle.Items.Add("Strikeout");
			this.cboxStyle.Items.Add("Underline");
			this.cboxStyle.Location = new System.Drawing.Point(168, 0);
			this.cboxStyle.Size = new System.Drawing.Size(72, 22);
			this.cboxStyle.SelectedIndexChanged += new System.EventHandler(this.cboxStyle_SelectedIndexChanged);
			// 
			// m_btnCancel
			// 
			this.m_btnCancel.Location = new System.Drawing.Point(105, 262);
			this.m_btnCancel.Name = "m_btnCancel";
			this.m_btnCancel.Size = new System.Drawing.Size(50, 19);
			this.m_btnCancel.Text = "Cancel";
			this.m_btnCancel.Click += new System.EventHandler(this.m_btnCancel_Click);
			// 
			// debugEnabled
			// 
			this.debugEnabled.Location = new System.Drawing.Point(12, 250);
			this.debugEnabled.Name = "debugEnabled";
			this.debugEnabled.Size = new System.Drawing.Size(172, 20);
			this.debugEnabled.Text = "Debug Logging";
			// 
			// FrmSettings
			// 
			this.ClientSize = new System.Drawing.Size(292, 326);
			this.Controls.Add(this.debugEnabled);
			this.Controls.Add(this.m_btnCancel);
			this.Controls.Add(this.cboxStyle);
			this.Controls.Add(this.textSample);
			this.Controls.Add(this.cboxSize);
			this.Controls.Add(this.cboxFont);
			this.Menu = this.m_mnuMain;
			this.Name = "FrmSettings";
			this.Text = "Preferences";
			this.Resize += new System.EventHandler(this.FrmSettings_Resize);
			this.Load += new System.EventHandler(this.FrmSettings_Load);
			this.ResumeLayout(false);
        }
		#endregion


        private void FrmSettings_Load(object sender, System.EventArgs e)
        {
        	Oxbow.Tools.TextLogger.Log("FrmSettings_Load() " );

        	this.ControlBox = true;
			this.MinimizeBox = false;

            this.debugEnabled.Top = this.textSample.Top + this.textSample.Height + debugEnabled.Height;

            m_btnCancel.Top = this.Height - (m_btnCancel.Height*2);
            m_btnCancel.Left = (this.Width / 2) - (m_btnCancel.Width / 2);

			//ArrayList fontList = CommonFunctions.GetMyFontList;
			//for (int i = 0; i < fontList.Count; i++)
			//{
			//	cboxFont.Items.Add(fontList[i]); 
			//}

			// Add font face names to cboxFont combo box
			try
			{ 
        		Oxbow.Tools.TextLogger.Log("FrmSettings_Load() getting font list" );
				ArrayList fontList = CommonFunctions.GetSystemFontList;

        		Oxbow.Tools.TextLogger.Log("FrmSettings_Load() got font list" );
				// Loop through collection, adding all items to combo box
				for (int i = 0; i < fontList.Count; i++)
				{
					cboxFont.Items.Add(fontList[i]); 
				}

				fontList = null;
			
				// Set initial font name, size, and style.
				cboxFont.SelectedIndex = 0;
				cboxSize.SelectedIndex = 0;
				cboxStyle.SelectedIndex = 0;
			}
			catch (Exception ex)
			{
				string str = "Error: cannot create FontCollection" +
							" -- make sure fontlist.dll is installed [" + 
						    ex.Message + "]";
        		Oxbow.Tools.TextLogger.Log("FrmSettings_Load() ["+str+"]" );
				MessageBox.Show(str, "FontPicker");
			}
		}


      private void SetFont()
      {
         int iFont = cboxFont.SelectedIndex;
         int iSize = cboxSize.SelectedIndex;
         int iStyle = cboxStyle.SelectedIndex; 
         if (iFont != -1 && iSize != -1 && iStyle != -1)
         {
            string strFont = (string)cboxFont.Items[iFont];
            string strSize = (string)cboxSize.Items[iSize];
            iSize = int.Parse(strSize);
            
            FontStyle fs;
            if (iStyle == 0)
            {
            	fs = FontStyle.Regular;}
            else if (iStyle == 1){
            	fs = FontStyle.Bold;}
            else if (iStyle == 2){
            	fs = FontStyle.Italic;}
            else if (iStyle == 3){
            	fs = FontStyle.Strikeout;}
            else{
            	fs = FontStyle.Underline;}

            // Create a new font.
            Font font = new Font(strFont, (float)iSize, fs);

            // Connect font to textbox
            textSample.Font = font;

            // Clean up font.
            font.Dispose();
         }
      }

      private void 
      cboxFont_SelectedIndexChanged(object sender, EventArgs e)
      {
         SetFont();
      }

      private void 
      cboxSize_SelectedIndexChanged(object sender, EventArgs e)
      {
         SetFont();
      }

      private void 
      cboxStyle_SelectedIndexChanged(object sender, EventArgs e)
      {
         SetFont();
      }


        private void FrmSettings_Resize(object sender, System.EventArgs e)
        {
        	Oxbow.Tools.TextLogger.Log("FrmSettings_Resize() " );
        	
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            this.Height = Screen.PrimaryScreen.Bounds.Height;
            if (Screen.PrimaryScreen.Bounds.Width > Screen.PrimaryScreen.Bounds.Height)
            {
                //Landscape();
            }
            else
            {
                //Portrait();
            }
            
            m_btnCancel.Top = this.Height - (m_btnCancel.Height*2);
        }

	}//class()
}//namespace
